package com.chensi.socket;

import java.io.IOException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.socket.TextMessage;

import com.chensi.common.AjaxJson;
import com.chensi.common.Global;
import com.chensi.util.DateUtils;

@Controller
@RequestMapping("msg")
public class MessageController {

	@Autowired
	MyWebSocketHandler handler;

	@RequestMapping("broadcast.json")
	@ResponseBody
	public AjaxJson broadcast(String text) {
		MessageReceive m = new MessageReceive();
		m.setEmit("chatMessage");
		Msg data = new Msg();
		data.setUsername("Z_子晴");
		data.setAvatar("http://tva3.sinaimg.cn/crop.0.0.512.512.180/8693225ajw8f2rt20ptykj20e80e8weu.jpg");
		data.setId("108101");
		data.setType("friend");
		data.setContent("测试信息"+DateUtils.getNowTime());
		data.setMine(false);
		data.setTimestamp(System.currentTimeMillis());
		m.setData(data);
		try {
			handler.broadcast(new TextMessage(Global.gson.toJson(m)));
		} catch (IOException e) {
			e.printStackTrace();
			return AjaxJson.getFailInfo();
		}
		return AjaxJson.getSuccessInfo();
	}
}
